package com.baizan.daka.ui.presenter

import com.baizan.daka.base.BasePresenter
import com.baizan.daka.event.TastMrgEvent
import com.baizan.daka.mvp.model.TaskModel
import com.baizan.daka.net.bean.PublishBean
import com.baizan.daka.rx.Subscriber.CommonSubscriber
import com.baizan.daka.showToast
import com.baizan.daka.ui.contract.PublishContract
import org.greenrobot.eventbus.EventBus

class PublishPresenter(val mView: PublishContract.View) : BasePresenter(),
    PublishContract.Presenter {

    val taskMode: TaskModel by lazy { TaskModel() }
    override fun publishTask(publishBean: PublishBean) {
        mView.showLoadingDialog()
        taskMode.publishTask(publishBean)
            .subscribe(object : CommonSubscriber<String>(compositeDisposable) {
                override fun onCallNext(t: String?) {
                    mView.publishTaskSuccess()
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

            })


    }

    override fun upOrDown(enabledFlag: Int, id: String,videoUrl:String? ) {
        mView.showLoadingDialog()
        taskMode.upOrDown(enabledFlag, id,videoUrl)
            .subscribe(object : CommonSubscriber<Any>(compositeDisposable) {
                override fun onCallNext(t: Any?) {
                    EventBus.getDefault().post(TastMrgEvent(id))
                    if (enabledFlag == 1) {
                        mView.upSuccess()
                    } else {
                        mView.downSuccess()
                    }
                }

                override fun onCallCompleted() {
                    super.onCallCompleted()
                    mView.hideLoadingDialog()
                }

                override fun onCallError(e: String) {
                    super.onCallError(e)
                    showToast(e)
                }

            })
    }


}

